package cn.aitrox.ry.app.web.controller.user;

import cn.aitrox.ry.app.web.vo.user.in.UserListInVo;
import cn.aitrox.ry.app.web.vo.user.out.UserListOutVo;
import cn.aitrox.ry.app.web.vo.user.out.UserLoginOutVo;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.service.api.dto.User;
import cn.aitrox.ry.service.user.dto.in.UserCreateInDto;
import cn.aitrox.ry.service.user.dto.in.UserEditInDto;
import cn.aitrox.ry.service.user.dto.in.UserForgetPwdInDto;
import cn.aitrox.ry.service.user.dto.in.UserListInDto;
import cn.aitrox.ry.service.user.dto.in.UserLoginInDto;
import cn.aitrox.ry.service.user.dto.in.UserSearchInDto;
import cn.aitrox.ry.service.user.feign.UserFeign;
import cn.aitrox.ry.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    @Lazy
    private UserFeign userFeign;

//    @Autowired
//    @Lazy
//    private AuthUserResourceFeign authUserResourceFeign;

    @PostMapping(value = "/user")
    public RespResult create(@Validated @RequestBody UserCreateInDto input) {
        return userFeign.create(input);
    }

    @PutMapping(value = "/user")
    public RespResult edit(@Validated @RequestBody UserEditInDto input) {
        return userFeign.edit(input);
    }

    @PostMapping(value = "/user/search")
    public RespResult<CommonPageInfo<User>> search(@Validated @RequestBody UserSearchInDto input) {
        CommonPageInfo<User> pageInfo = userFeign.search(input);
        return RespResult.ok(pageInfo);
    }

    @PostMapping(value = "/user/login")
    public RespResult<UserLoginOutVo> login(@Validated @RequestBody UserLoginInDto input) {
        RespResult<User> respResult = userFeign.login(input);
        if (!respResult._isOk()) {
            return RespResult.secByError(respResult.getCode(), respResult.getMsg());
        }

        User user = respResult.getData();

//        // 查询用户权限资源
//        RespResult<AuthUserResourceListOutDto> resourceResp = authUserResourceFeign.list(user.getId());
//        if (!respResult._isOk()) {
//            return RespResult.secByError(resourceResp.getCode(), resourceResp.getMsg());
//        }

        UserLoginOutVo result = new UserLoginOutVo();

        BeanUtil.copy(user, result);

//        if (!ArrayUtil.isEmpty(resourceResp.getData().getResourceList())) {
//            List<AuthResourceDetailOutVo> reourceVoList = resourceResp.getData().getResourceList().stream().map(x -> AuthResourceDetailOutVo.mapAuthResourceDetailOutVo(x)).collect(Collectors.toList());
//            result.setResourceList(reourceVoList);
//        }

        return RespResult.ok(result);
    }

    @PutMapping(value = "/user/forget/pwd")
    public RespResult forgetPwd(@Validated @RequestBody UserForgetPwdInDto input) {
        return userFeign.forgetPwd(input);
    }

    @PostMapping(value = "/user/list")
    public RespResult<UserListOutVo> list(@Validated @RequestBody UserListInVo input) {
        List<User> list = userFeign.list(new UserListInDto(input.getIds()));
        return RespResult.ok(new UserListOutVo(list));
    }

}
